package com.zzyl.nursing.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.dto.NursingPlanDTO;
import com.zzyl.nursing.domain.vo.NursingPlanVo;
import com.zzyl.nursing.domain.vo.NursingProjectPlanVo;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
@Slf4j
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //我们需要两个信息，一个是护理计划信息，一个是护理计划相关的护理项目信息。
        // 1.查询护理计划信息，封装进nursingPlanVo
        NursingPlan byId = getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(byId,nursingPlanVo);//待解决，这里想把Long类型id转换成String

        //2.查询护理计划对应的护理项目计划关系，封装进nursingPlanVo
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO dto)
    {
        //这里我们有两个工作要做，一是添加护理计划信息，而是将护理计划和对应的项目，
        //首先将护理计划信息添加到护理计划表中，这里是new了一个新的对象，将dto中的数据复制到nursingPlan中
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //批量保存护理项目计划关系
        //mybatis-plus这个springboot2的版本自带的insert，没有提供批量保存
        //所以这里自定义一个批量保存方法，mybatis手搓一下
        //参数如何考虑呢?对比一下DTO和实体类，除自动填充的之外都全，所以决定用DTO中的集合
        //这里需要对集合中的每一个关联元素，绑定好其对应的planid
        for (NursingProjectPlan nursingProjectPlan : dto.getProjectPlans()) {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }
        nursingProjectPlanMapper.insertBatch(dto.getProjectPlans());

        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO dto)
    {
        try {
            //根据工作流程，先是将DTO中的数据复制到实体类中，方便对数据库操作
            NursingPlan nursingPlan = new NursingPlan();
            log.info("开始复制将dto转化为实体类 !!!!!!");
            BeanUtils.copyProperties(dto,nursingPlan);
            //然后判断，这个dto中有没有projectPlans，没有直接修改护理计划即可，将其关联的所有项目删除
            if (dto.getProjectPlans() == null || dto.getProjectPlans().size() == 0) {
                nursingProjectPlanMapper.deleteByPlanId(dto.getId());
                //TODO 这里按理说是公共自动填充的，但是貌似新增的时候就没有自动填充
                nursingPlan.setUpdateTime(DateUtils.getNowDate());
                return updateById(nursingPlan) ? 1 : 0;//清理完后更新
            }

            //如果有projectPlans，接下来处理关联的项目
            //新增之前，需要先通过当前计划的id，删除关联表中所有关联项目信息
            //没有这个方法，手搓一个
            log.info("开始删除计划关联项目 !!!!!!");
            nursingProjectPlanMapper.deleteByPlanId(dto.getId());

            for (NursingProjectPlan nursingProjectPlan : dto.getProjectPlans()) {
                //和之前新增的逻辑一样，先绑定planid
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                //第一次测试之后发现问题，原本的没删除掉，结果还新增了一个
                //这里先将它的id设置为原本的 id
    //            nursingProjectPlan.setId(nursingProjectPlan.getId());
            }
            //开始批量新增逻辑，当时写了一个insertBatch方法
            log.info("开始批量新增对应的关联内容 !!!!!!");
            nursingProjectPlanMapper.insertBatch(dto.getProjectPlans());

            return updateById(nursingPlan) ? 1 : 0;
        } catch (Exception e) {
            log.error("修改护理计划异常",e);
            throw e;
        }
    }

/*    @Override
    public int updateNursingPlan(NursingPlanDTO dto)
    {
        try {
            // 属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);
            // 删除护理计划对应的护理项目列表
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            // 判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
            if (dto.getProjectPlans() != null && !dto.getProjectPlans().isEmpty()) {

                // 补全护理计划id
                List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
                projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingProjectPlan.getId()));
                // 批量保存护理计划对应的护理项目列表
                nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());
            }

            // 不管项目列表是否为空，都要修改护理计划
            return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }*/

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        //删除护理计划，首先得先删除关联表中的护理计划和其关联的相关护理项目
        //这里也是根据planid批量删除nursing_project_plan表中的数据
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanIds(ids);
        //清楚完关联数据，删除护理计划
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> all = nursingPlanMapper.getAll();
        return all;
    }
}